﻿using MrPP.SocketSystem;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace MrPP.SerriedCabinet
{
    public class RespActiveColumnPosition : GDGeek.Singleton<RespActiveColumnPosition>, Resp
    {

        public Action<Data> onRecevie
        {
            get;
            set;
        }

        public class Data
        {
            public byte area;
            public ushort[] positions;
        }
        void Start()
        {
            Debug.Log(getCommand());
            ProtoManager.Instance.addResp(this);
        }
        void OnDestroy()
        {
            if (ProtoManager.IsInitialized)
            {
                ProtoManager.Instance.removeResp(this);
            }
        }
        public void deserialize(DataStream reader)
        {
            Data data = new Data();
            data.area = reader.ReadByte();

            data.positions = new ushort[GlobalConfigure.Instance.getCount(data.area)];
            for (int i = 0; i < data.positions.Length; ++i) {
                data.positions[i] = reader.ReadByte();
            }

            Debug.Log(JsonUtility.ToJson(data));
            onRecevie?.Invoke(data);
        }

        public short getCommand()
        {
            var bytes = new byte[] { 0xFE, 0xED };
            return BitConverter.ToInt16(bytes, 0);
        }

        public void serialize(DataStream writer)
        {//
         // throw new System.NotImplementedException();
        }
    }
}